package com.learn.springcloud.ribboncodingconfigconsumer.controller;

import com.learn.springcloud.config.ConsumerRibbonClientConfiguration1;
import com.learn.springcloud.config.ConsumerRibbonClientConfiguration2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Time : 2018/7/25 9:51
 * @Version : 1.0
 */
@RestController
public class Consumer {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
     * 测试自定义路由规则
     * @return
     */
    @RequestMapping(value = "/custom/route/rule")
    public String customIRule(){
        String result = "";
        ServiceInstance serviceInstance1 = loadBalancerClient.choose("ribbon-coding-config-provider-group1");
        ServiceInstance serviceInstance2 = loadBalancerClient.choose("ribbon-coding-config-provider-group2");
        ServiceInstance serviceInstance3 = loadBalancerClient.choose("ribbon-coding-config-provider-group3");

        System.out.println(serviceInstance1);
        System.out.println(serviceInstance2);
        System.out.println(serviceInstance3);

        result = serviceInstance1.toString() + "\n" + serviceInstance2.toString() + "\n" + serviceInstance3.toString();

        return result;
    }
}
